package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.dto.DishDto;
import com.example.dto.SetmealDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.Setmeal;
import com.example.service.SetmealService;
import com.example.service.impl.CategoryServiceImpl;
import com.example.service.impl.DishServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐(Setmeal)表控制层
 *
 * @author makejava
 * @since 2022-05-23 14:39:52
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    /**
     * 服务对象
     */
    @Resource
    private SetmealService setmealService;
    @Resource
    private CategoryServiceImpl categoryService;
    @Resource
    private DishServiceImpl dishService;
    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @PostMapping
    public R<String>postSetmeal(@RequestBody SetmealDto setmealDto){
        int a=setmealService.saveSetmeal(setmealDto);
        if(a==0){
            return R.success("成功");
        }else{
            return R.error("失败");
        }

    }
    @GetMapping("/page")
    public R<Page>getpage(int page,int pageSize,String name){
        Page<Setmeal> page1=new Page(page,pageSize);
        Page<SetmealDto>page2=new Page<>();
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        setmealService.page(page1,queryWrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Setmeal> setmealList=page1.getRecords();
        List<SetmealDto>setmealDtos=setmealList.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category=categoryService.getById(item.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(setmealDtos);
        return R.success(page2);
    }
    @DeleteMapping
    public R<String>deleteSetmeal(String ids) {
        String arr[] = ids.split(",");
        for (int i = 0; i < arr.length; i++) {
            int a = setmealService.deleteSetmeal(Long.parseLong(arr[i]));
            setmealService.removeById(Long.parseLong(arr[i]));
        }


        return R.success("删除成功");


    }
    @PostMapping("/status/{status}")
    public R<String>updatestatus(@PathVariable int status,String ids) {
        String arr[] = ids.split(",");
        for (int i = 0; i < arr.length; i++) {
            Setmeal setmeal = setmealService.getById(Long.parseLong(arr[i]));
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("停售处理成功");


    }
    @GetMapping("list")
    public R<List<Setmeal>>getList(int status,Long categoryId){
        System.out.println("status"+status+"categoryId"+categoryId);
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,status);
        List<Setmeal>setmealList=setmealService.list(queryWrapper);
        return R.success(setmealList);
    }
    @GetMapping("/dish/{id}")
    public R<DishDto>getDish(@PathVariable Long id){
        DishDto dto=dishService.getDishDtoById(id);
        return R.success(dto);
    }

}